package com.meixiang.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.entity.collect.CollectDateEntity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.tool.Tool;
import com.meixiang.view.OldPriceView;
import com.meixiang.view.SlideHorizontalView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * author:xiejingwen
 */
public class CollectAdapter extends com.meixiang.recyclerview.RecyclerAdapter<CollectAdapter.ViewHolder> {
    private Context mContext;
    public boolean isShow;
    public int showPosition;
    public boolean close;
    private List<CollectDateEntity> mCollectList=new ArrayList<>();
    private Activity mActivity ;
    public CollectAdapter(Context context, Activity mActivity){
        this.mContext = context ;
        this.mActivity = mActivity ;
    }
    public void addData(List<CollectDateEntity> collectList){
        mCollectList.clear();;
        mCollectList.addAll(collectList);
        notifyDataSetChanged();
    }
    public void clearData(){
        mCollectList.clear();;
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_collect, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.mTvTitle.setText(mCollectList.get(position).getFavName());
        holder.mTvMoney.setText("￥" + Math.round(Double.parseDouble(mCollectList.get(position).getFavStorePrice())));
        holder.mTvOldPrice.setText("￥" + Math.round(Double.parseDouble(mCollectList.get(position).getFavMarketPrice())));

        if ("2".equals(mCollectList.get(position).getFavType())) { //商品收藏
            holder.mTvBtn.setText("预约服务");
        } else if ("1".equals(mCollectList.get(position).getFavType())) {
            holder.mTvBtn.setText("加入购物车");
        }
        if (isShow) {
            if (position != showPosition) {
                holder.mLlCancelCollect.setVisibility(View.GONE);
            } else {
                holder.mLlCancelCollect.setVisibility(View.VISIBLE);
                if (close) {
                    holder.mViewSlide.setShowView(false);
                }
            }
        } else {
            holder.mLlCancelCollect.setVisibility(View.VISIBLE);
            //holder.mViewSlide.setShowView(isShow);
        }
        holder.mViewSlide.setOpenOrCloseListener(new SlideHorizontalView.IOpenOrClose() {
            @Override
            public void getStatus(boolean isOpen) {
                isShow = isOpen;
                showPosition = position;
                notifyDataSetChanged();
            }
        });

        holder.mLlCancelCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelMatch(position);
            }
        });
    }
    @Override
    public int getItemCount() {
        return mCollectList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.iv_img)
        ImageView mIvImg;
        @Bind(R.id.tv_title)
        TextView mTvTitle;
        @Bind(R.id.tv_money)
        TextView mTvMoney;
        @Bind(R.id.tv_old_price)
        OldPriceView mTvOldPrice;
        @Bind(R.id.tv_btn)
        TextView mTvBtn;
        @Bind(R.id.ll_cancel_collect)
        LinearLayout mLlCancelCollect;
        @Bind(R.id.view_slide)
        SlideHorizontalView mViewSlide;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 取消收藏
     */
    private void cancelMatch(final int position){
            HttpParams params = new HttpParams();
            params.put("favType",mCollectList.get(position).getFavType());
            params.put("favId","1001");
            HttpUtils.post(Config.COLLECT_LIST, params, new HttpCallBack(mActivity) {
                @Override
                public void onSucceed(JSONObject result, String code, String message) {
                    Tool.showTextToast(mContext,"取消收藏成功");
                }

                @Override
                public void onError(String code, String message) {

                }
            });
    }

}
